package app.modules.security.persistence.dao;

import app.modules.security.dto.biz.AssignedFunctionDTO;
import app.modules.security.dto.web.function.WebFunctionDTO;
import app.modules.security.dto.web.function.WebFunctionQueryDTO;
import app.modules.security.persistence.mapper.FunctionMapper;
import app.modules.security.util.conversion.FunctionConvertUtil;
import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import jasmine.framework.common.util.CheckUtil;
import jasmine.framework.common.util.StringUtil;
import jasmine.framework.database.mybatisplus.dao.BaseI18nEntityDAO;
import jasmine.framework.database.mybatisplus.wrapper.LambdaQueryWrapperWithI18n;
import jasmine.security.rbac.model.SecFunction;
import org.springframework.stereotype.Repository;

import javax.annotation.Nullable;
import java.util.Collection;
import java.util.List;

/**
 * @author mh.z
 */
@Repository
public class FunctionDAO extends BaseI18nEntityDAO<FunctionMapper, SecFunction> {

    /**
     * 分页查询功能
     *
     * @param queryDTO
     * @param page
     * @return
     */
    public List<WebFunctionDTO> pageWebFunctionDTOsByCond(WebFunctionQueryDTO queryDTO, Page page) {
        CheckUtil.notNull(queryDTO, "queryDTO null");
        CheckUtil.notNull(page, "page null");
        LambdaQueryWrapperWithI18n<SecFunction> wrapper = LambdaQueryWrapperWithI18n.wrapper();

        String functionCode = queryDTO.getFunctionCode();
        wrapper.like(StringUtil.isNotEmpty(functionCode), SecFunction::getFunctionCode, functionCode);
        String functionName = queryDTO.getFunctionName();
        wrapper.likeWithI18n(StringUtil.isNotEmpty(functionName), SecFunction::getFunctionName, functionName);
        String remark = queryDTO.getRemark();
        wrapper.likeWithI18n(StringUtil.isNotEmpty(remark), SecFunction::getRemark, remark);
        wrapper.orderByAsc(SecFunction::getFunctionCode);

        Page<SecFunction> pageResult = baseMapper.selectPageWithI18n(page, wrapper);
        List<SecFunction> recordList = pageResult.getRecords();
        List<WebFunctionDTO> returnList = FunctionConvertUtil.secFunction2WebFunctionDTO(recordList);

        return returnList;
    }

    /**
     * 查询指定用户的所有分配的功能
     *
     * @param userId
     * @return
     */
    public List<AssignedFunctionDTO> listAssignedFunctionsByUserId(Long userId) {
        return baseMapper.listAssignedFunctionsByUserId(userId);
    }

    /**
     * 根据功能代码统计
     *
     * @param functionCode
     * @param excludeIds
     * @return
     */
    public long countByFunctionCode(String functionCode, @Nullable Collection<Long> excludeIds) {
        LambdaQueryWrapper<SecFunction> wrapper = Wrappers.lambdaQuery();

        wrapper.eq(SecFunction::getFunctionCode, functionCode);
        wrapper.notIn(CollectionUtil.isNotEmpty(excludeIds), SecFunction::getId, excludeIds);

        return baseMapper.selectCount(wrapper);
    }

}
